package com.shanlitech.et.servlet;

import com.google.protobuf.DynamicMessage;
import com.shanlitech.et.SpeechRecorder;
import com.shanlitech.et.Bootstrap;
import com.shanlitech.et.pb.Agpub;
import org.springframework.util.StringUtils;
import org.zeromq.ZMQ;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.UUID;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@WebServlet
public class StressServlet extends HttpServlet {

    protected void doGet(HttpServletRequest request,
                         HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    protected void doPost(HttpServletRequest request,
                          HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("utf-8");

        String Tcount = request.getParameter("tcount");
        String Sum = request.getParameter("sum");
        Integer tcount;
        final Integer sum;
        final ZMQ.Socket pub = Bootstrap.pub;

        if(StringUtils.hasText(Tcount)) {
            tcount = Integer.parseInt(Tcount);
        } else {
            tcount =5;
        }
        if(StringUtils.hasText(Sum)) {
            sum = Integer.parseInt(Sum);
        } else {
            sum = 10;
        }

        final SpeechRecorder recorder = SpeechRecorder.getInstance();
        long start = System.currentTimeMillis();

        /*for(int j=0;j<sum;j++) {
            Long sid = 6612403000000000000l + uuid();
            recorder.zpb.send(pub, onMemberGotMic(sid));
            recorder.zpb.send(pub, onAudioPacket(sid));
            recorder.zpb.send(pub, onAudioPacket(sid));
            recorder.zpb.send(pub, onAudioPacket(sid));
            recorder.zpb.send(pub, onAudioPacket(sid));
            recorder.zpb.send(pub, onAudioPacket(sid));
            recorder.zpb.send(pub, onAudioPacket(sid));
            recorder.zpb.send(pub, onAudioPacket(sid));
            recorder.zpb.send(pub, onAudioPacket(sid));
            recorder.zpb.send(pub, onMemberLostMic(sid));

            System.out.println("pub msg " + sid);
        }*/
        ExecutorService executorService = Executors.newCachedThreadPool();
        for(int i=0;i<tcount;i++) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {}
            executorService.execute(new Runnable() {
                public void run() {
                    for(int j=0;j<sum;j++) {
                        Long sid = 6612403000000000000l + uuid();
                        recorder.zpb.send(pub, onMemberGotMic(sid));
                        recorder.zpb.send(pub, onAudioPacket(sid));
                        recorder.zpb.send(pub, onAudioPacket(sid));
                        recorder.zpb.send(pub, onAudioPacket(sid));
                        recorder.zpb.send(pub, onAudioPacket(sid));
                        recorder.zpb.send(pub, onAudioPacket(sid));
                        recorder.zpb.send(pub, onAudioPacket(sid));
                        recorder.zpb.send(pub, onAudioPacket(sid));
                        recorder.zpb.send(pub, onAudioPacket(sid));
                        recorder.zpb.send(pub, onMemberLostMic(sid));

                        System.out.println("pub msg " + sid);
                    }
                }
            });
        }
        //countDownLatch.await();
        executorService.shutdown();
        while(true){
            if(executorService.isTerminated()){
                System.out.println("所有线程执行完毕");
                break;
            }
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {}
        }

        long end = System.currentTimeMillis();
        System.out.println(tcount+"个线程执行"+sum+"次, 耗时"+(end-start)+"ms");
    }

    static DynamicMessage onMemberGotMic(Long speech_id) {
        /*fd_gotmic_ag_id;
        fd_gotmic_speech_id;
        fd_gotmic_gid;
        fd_gotmic_uid;
        fd_gotmic_payload;*/
        DynamicMessage.Builder builder = DynamicMessage.getDefaultInstance(Agpub.MemberGotMic.getDescriptor()).newBuilderForType();
        SpeechRecorder recorder = SpeechRecorder.getInstance();
        builder.setField(recorder.fd_gotmic_ag_id, "ag-1-1");
        builder.setField(recorder.fd_gotmic_speech_id, speech_id);
        builder.setField(recorder.fd_gotmic_gid, 6600039741105635330l);
        builder.setField(recorder.fd_gotmic_uid, 33);
        builder.setField(recorder.fd_gotmic_payload, 103);
        return builder.build();
    }

    static DynamicMessage onAudioPacket(Long speech_id) {
        /*fd_ap_ag_id;
        fd_ap_speech_id;
        fd_ap_gid;
        fd_ap_uid;
        fd_ap_payload;
        fd_ap_seq;
        fd_ap_ts;
        fd_ap_data;*/
        DynamicMessage.Builder builder = DynamicMessage.getDefaultInstance(Agpub.AudioPacket.getDescriptor()).newBuilderForType();
        SpeechRecorder recorder = SpeechRecorder.getInstance();
        builder.setField(recorder.fd_ap_ag_id, "ag-1-1");
        builder.setField(recorder.fd_ap_speech_id, speech_id);
        builder.setField(recorder.fd_ap_gid, 6600039741105635330l);
        builder.setField(recorder.fd_ap_uid, 33);
        builder.setField(recorder.fd_ap_payload, 103);
        builder.setField(recorder.fd_ap_data, new byte[]{(byte)0x64, (byte)0x33, (byte)0xdb, (byte)0xaa, (byte)0xe2, (byte)0x3d,
                0x37, 0x55, (byte)0xc1, 0x7a, (byte)0x93, 0x6e, 0x10, (byte)0xbd, 0x20, 0x54, 0x37, (byte)0xd3, (byte)0xa3, (byte)0xd2, 0x12, (byte)0x80});
        return builder.build();
    }

    static DynamicMessage onMemberLostMic(Long speech_id) {
        /*fd_lostmic_ag_id;
        fd_lostmic_speech_id;
        fd_lostmic_gid;
        fd_lostmic_uid;
        fd_lostmic_payload;*/
        DynamicMessage.Builder builder = DynamicMessage.getDefaultInstance(Agpub.MemberLostMic.getDescriptor()).newBuilderForType();
        SpeechRecorder recorder = SpeechRecorder.getInstance();
        builder.setField(recorder.fd_lostmic_ag_id, "ag-1-1");
        builder.setField(recorder.fd_lostmic_speech_id, speech_id);
        builder.setField(recorder.fd_lostmic_gid, 6600039741105635330l);
        builder.setField(recorder.fd_lostmic_uid, 33);
        builder.setField(recorder.fd_lostmic_payload, 103);
        return builder.build();
    }

    public static Long uuid() {
        int i = UUID.randomUUID().hashCode();
        if(i < 0) {
            i = -i;
        }
        return new Integer(i).longValue();
    }
}
